# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\status_cache\manual_browsing_requests_cache.py
import logging, time
from threading import RLock

_logger = logging.getLogger("helpers.status_cache.manual_browsing")


class ManualBrowsingStatusCache:
    def __init__(self):
        self._ManualBrowsingStatusCache__lock = RLock()
        self._ManualBrowsingStatusCache__scan_session_storage = dict()
        self._ManualBrowsingStatusCache__ui_session_filter = dict()
        self._ManualBrowsingStatusCache__scan_id_to_ui_session_id = dict()

    def register_ui_session_id(self, scan_id, ui_session_id):
        with self._ManualBrowsingStatusCache__lock:
            self._ManualBrowsingStatusCache__scan_id_to_ui_session_id[
                scan_id
            ] = ui_session_id

    def get_ui_session_id(self, scan_id):
        with self._ManualBrowsingStatusCache__lock:
            return self._ManualBrowsingStatusCache__scan_id_to_ui_session_id.get(
                scan_id
            )

    def init_item(
        self,
        user_id,
        ui_session_id,
        scan_session_id,
        scanning_app,
        target_id,
        scan_id,
        address,
        description,
    ):
        scan_session_key = "%s-%s" % (scan_session_id, scanning_app)
        ui_session_key = "%s-%s" % (user_id, ui_session_id)
        with self._ManualBrowsingStatusCache__lock:
            self._ManualBrowsingStatusCache__scan_session_storage[
                scan_session_key
            ] = dict(
                user_id=user_id,
                ui_session_id=ui_session_id,
                scan_session_id=scan_session_id,
                scanning_app=scanning_app,
                target_id=target_id,
                scan_id=scan_id,
                created=time.time(),
                updated=time.time(),
                old=0,
                index=0,
                target_desc=(address, description),
            )
            if ui_session_key not in self._ManualBrowsingStatusCache__ui_session_filter:
                self._ManualBrowsingStatusCache__ui_session_filter[
                    ui_session_key
                ] = set()
            self._ManualBrowsingStatusCache__ui_session_filter[ui_session_key].add(
                scan_session_key
            )

    def update_current_manual_browsing_event(
        self, scan_session_id, scanning_app, data=None
    ):
        """
        :param scan_session_id:
        :param scanning_app:
        :param data:
        :return: return's true if there was a previous valid event
        """
        scan_session_key = "%s-%s" % (scan_session_id, scanning_app)
        result = False
        with self._ManualBrowsingStatusCache__lock:
            entry = self._ManualBrowsingStatusCache__scan_session_storage.get(
                scan_session_key
            )
            if entry:
                if entry.get("data", dict()):
                    result = True
                if data is not None:
                    entry["index"] += 1
                entry["updated"] = time.time()
                entry["data"] = data
                _logger.debug(
                    "manual browsing data entry %s updated %s", scan_session_key, entry
                )
            else:
                _logger.debug("warning manual browsing data entry %s not found")
        return result

    def remove_item(self, scan_session_id, scanning_app):
        scan_session_key = "%s-%s" % (scan_session_id, scanning_app)
        with self._ManualBrowsingStatusCache__lock:
            entry = self._ManualBrowsingStatusCache__scan_session_storage.get(
                scan_session_key
            )
            if entry:
                ui_session_key = "%s-%s" % (entry["user_id"], entry["ui_session_id"])
                self._ManualBrowsingStatusCache__ui_session_filter[
                    ui_session_key
                ].remove(scan_session_key)
                del self._ManualBrowsingStatusCache__scan_session_storage[
                    scan_session_key
                ]

    def get_user_events(self, user_id, ui_session_id):
        current_time = time.time()
        ui_session_key = "%s-%s" % (user_id, ui_session_id)
        result = []
        with self._ManualBrowsingStatusCache__lock:
            scan_session_keys = self._ManualBrowsingStatusCache__ui_session_filter.get(
                ui_session_key, set()
            )
            for scan_session_key in scan_session_keys:
                entry = self._ManualBrowsingStatusCache__scan_session_storage.get(
                    scan_session_key
                )
                if entry:
                    if entry.get("data"):
                        entry["old"] = current_time - entry["created"]
                        result.append(dict(entry))

        return result
